package com.yuanxin.contacts;

import android.database.Cursor;
import android.provider.ContactsContract;
import android.text.TextUtils;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
//import com.github.promeg.pinyinhelper.Pinyin;

import java.util.ArrayList;
import java.util.List;

public class Contact {
    public String contactId;
    public String displayName;
    public String givenName = "";
    public String middleName = "";
    public String familyName = "";
    public String prefix = "";
    public String suffix = "";
    public String company = "";
    public String jobTitle = "";
    public String department = "";
    public boolean hasPhoto = false;
    public String photoUri;
    public List<Item> emails = new ArrayList<>();
    public List<Item> phones = new ArrayList<>();
    public List<PostalAddressItem> postalAddresses = new ArrayList<>();
    public Birthday birthday;

    private String firstNamePinYin;

    //获取姓氏的拼音
    public String getFirstNamePinYin() {
        return firstNamePinYin;
    }

    public void setFirstNamePinYin(String firstNamePinYin) {
        this.firstNamePinYin = firstNamePinYin;
    }

//    public String getPinYin() {
//        String pinyinName = "";
//        if (!TextUtils.isEmpty(prefix)) {
//            pinyinName += prefix;
//        }
//        if (!TextUtils.isEmpty(familyName)) {
//            pinyinName += familyName;
//        }
//        if (!TextUtils.isEmpty(middleName)) {
//            pinyinName += middleName;
//        }
//        if (!TextUtils.isEmpty(givenName)) {
//            pinyinName += givenName;
//        }
//        if (!TextUtils.isEmpty(suffix)) {
//            pinyinName += suffix;
//        }
//
//        String pinyin = Pinyin.toPinyin(pinyinName.trim(), "");
//        return pinyin;
//    }
    public String getFullName() {
        String fullName = "";
        if (!TextUtils.isEmpty(prefix)) {
            fullName += prefix.trim();
        }
        if (!TextUtils.isEmpty(familyName)) {
            fullName += familyName.trim();
        }
        if (!TextUtils.isEmpty(middleName)) {
            fullName += middleName.trim();
        }
        if (!TextUtils.isEmpty(givenName)) {
            fullName += givenName.trim();
        }
        if (!TextUtils.isEmpty(suffix)) {
            fullName += suffix.trim();
        }
        return fullName;
    }

    public Contact(String contactId) {
        this.contactId = contactId;
    }

    public WritableMap toMap() {
        WritableMap contact = Arguments.createMap();
        contact.putString("recordID", contactId);
        contact.putString("givenName", TextUtils.isEmpty(givenName) ? displayName : givenName);
        contact.putString("middleName", middleName);
        contact.putString("familyName", familyName);
        contact.putString("prefix", prefix);
        contact.putString("suffix", suffix);
        contact.putString("company", company);
        contact.putString("jobTitle", jobTitle);
        contact.putString("department", department);
        contact.putBoolean("hasThumbnail", this.hasPhoto);
        contact.putString("thumbnailPath", photoUri == null ? "" : photoUri);

        WritableArray phoneNumbers = Arguments.createArray();
        for (Item item : phones) {
            WritableMap map = Arguments.createMap();
            map.putString("number", item.value);
            map.putString("label", item.label);
            phoneNumbers.pushMap(map);
        }
        contact.putArray("phoneNumbers", phoneNumbers);

        WritableArray emailAddresses = Arguments.createArray();
        for (Item item : emails) {
            WritableMap map = Arguments.createMap();
            map.putString("email", item.value);
            map.putString("label", item.label);
            emailAddresses.pushMap(map);
        }
        contact.putArray("emailAddresses", emailAddresses);

        WritableArray postalAddresses = Arguments.createArray();
        for (PostalAddressItem item : this.postalAddresses) {
            postalAddresses.pushMap(item.map);
        }
        contact.putArray("postalAddresses", postalAddresses);

        WritableMap birthdayMap = Arguments.createMap();
        if (birthday != null) {
            birthdayMap.putInt("year", birthday.year);
            birthdayMap.putInt("month", birthday.month);
            birthdayMap.putInt("day", birthday.day);
            contact.putMap("birthday", birthdayMap);
        }

        return contact;
    }

    public static class Item {
        public String label;
        public String value;

        public Item(String label, String value) {
            this.label = label;
            this.value = value;
        }
    }

    public static class Birthday {
        public int year = 0;
        public int month = 0;
        public int day = 0;

        public Birthday(int year, int month, int day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }
    }

    public static class PostalAddressItem {
        public final WritableMap map;

        public PostalAddressItem(Cursor cursor) {
            map = Arguments.createMap();

            map.putString("label", getLabel(cursor));
            putString(cursor, "formattedAddress", ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
            putString(cursor, "street", ContactsContract.CommonDataKinds.StructuredPostal.STREET);
            putString(cursor, "pobox", ContactsContract.CommonDataKinds.StructuredPostal.POBOX);
            putString(cursor, "neighborhood", ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD);
            putString(cursor, "city", ContactsContract.CommonDataKinds.StructuredPostal.CITY);
            putString(cursor, "region", ContactsContract.CommonDataKinds.StructuredPostal.REGION);
            putString(cursor, "state", ContactsContract.CommonDataKinds.StructuredPostal.REGION);
            putString(cursor, "postCode", ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE);
            putString(cursor, "country", ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY);
        }

        private void putString(Cursor cursor, String key, String androidKey) {
            final String value = cursor.getString(cursor.getColumnIndex(androidKey));
            if (!TextUtils.isEmpty(value))
                map.putString(key, value);
        }

        static String getLabel(Cursor cursor) {
            switch (cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE))) {
                case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
                    return "home";
                case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
                    return "work";
                case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_CUSTOM:
                    final String label = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.LABEL));
                    return label != null ? label : "";
            }
            return "other";
        }
    }

}
